package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Transfer;
import com.zyd.shiro.business.service.PakTransferService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakMigrate;
import com.zyd.shiro.persistence.beans.PakTransfer;
import com.zyd.shiro.persistence.beans.SysUser;
import com.zyd.shiro.persistence.mapper.PakTransferMapper;
import lombok.extern.slf4j.Slf4j;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakTransferServiceImpl implements PakTransferService {

    @Autowired
    private PakTransferMapper pakTransferMapper;

    @Override
    public Transfer insert(Transfer transfer) {
        Assert.notNull(transfer, "对象不可为空！");
        PakTransfer pakTransfer = transfer.getPakTransfer();
        pakTransfer.setUnicode("DB"+System.currentTimeMillis());
        pakTransfer.setAppstatus(0);
        pakTransfer.setUpdateTime(new Date());
        pakTransfer.setCreateTime(new Date());
        pakTransferMapper.insertSelective(pakTransfer);
        return transfer;
    }

    @Override
    public void insertList(List<Transfer> transfers) {
            for(Transfer transfer:transfers){
               transfer.getPakTransfer().setUpdateTime(new Date());
               transfer.getPakTransfer().setCreateTime(new Date());
               pakTransferMapper.insertSelective(transfer.getPakTransfer());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakTransferMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Transfer transfer) {
        Assert.notNull(transfer, "对象不可为空！");
        transfer.setUpdateTime(new Date());
        return pakTransferMapper.updateByPrimaryKey(transfer.getPakTransfer()) > 0;
    }

    @Override
    public boolean updateSelective(Transfer transfer) {
        Assert.notNull(transfer, "对象不可为空！");
        transfer.setUpdateTime(new Date());
        return pakTransferMapper.updateByPrimaryKeySelective(transfer.getPakTransfer()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Transfer getByPrimaryKey(Long primaryKey) {
        PakTransfer pakTransfer = pakTransferMapper.selectByPrimaryKey(primaryKey);
        return null == pakTransfer ? null : new Transfer(pakTransfer);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Transfer getOneByEntity(Transfer test) {
        Assert.notNull(test, "对象不可为空！");
        PakTransfer pakTransfer = pakTransferMapper.selectOne(test.getPakTransfer());
        return null == pakTransfer ? null : new Transfer(pakTransfer);
    }

    @Override
    public List<Transfer> listAll() {
        List<PakTransfer> pakTransfers = pakTransferMapper.selectAll();
        if (CollectionUtils.isEmpty(pakTransfers)) {
            return null;
        }
        List<Transfer> transfers = new ArrayList<>();
        for (PakTransfer pakTransfer : pakTransfers) {
            transfers.add(new Transfer(pakTransfer));
        }
        return transfers;
    }

    @Override
    public List<Transfer> listByEntity(Transfer entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Transfer> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakTransfer> pakTransfers = pakTransferMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakTransfers)) {
            return null;
        }
        List<Transfer> transfers = new ArrayList<>();
        for (PakTransfer pakTransfer : pakTransfers) {
            transfers.add(new Transfer(pakTransfer));
        }
        PageInfo bean = new PageInfo<PakTransfer>(pakTransfers);
        bean.setList(transfers);
        return bean;
    }
@Override
    public void increase(PakTransfer pakTransfer) {
        pakTransferMapper.insert(pakTransfer);
    }




}
